// 公共验证器
export const validators = {
	required: (message) => ({ required: true, message, trigger: 'change' }),
	positiveNumber: (message) => ({
		validator: (_, value, callback) => {
			if (value && (isNaN(value) || value < 0)) {
				callback(new Error(message))
			} else {
				callback()
			}
		},
		trigger: 'blur',
	}),
	positiveStep: (message) => ({
		validator: (_, value, callback) => {
			if (value && (isNaN(value) || value <= 0)) {
				callback(new Error(message))
			} else {
				callback()
			}
		},
		trigger: 'blur',
	}),
	range01: (message) => ({
		validator: (_, value, callback) => {
			if (value !== null && (isNaN(value) || value < 0 || value > 1)) {
				callback(new Error(message))
			} else {
				callback()
			}
		},
		trigger: 'blur',
	}),
	step01: (message) => ({
		validator: (_, value, callback) => {
			if (value !== null && (isNaN(value) || value <= 0 || value > 1)) {
				callback(new Error(message))
			} else {
				callback()
			}
		},
		trigger: 'blur',
	}),
}

// 二元计算数据库配置
export const binaryDatabase = {
	elementRelations: {
		AL: ['AG', 'C', 'CU'],
		MG: ['ND', 'SI', 'ZN'],
	},
	validCombinations: [
		{
			components: ['AL', 'AG'],
			dbFile: 'Ag-Al.tdb',
			defaults: {
				xRange: { start: 0, stop: 1, step: 0.02 },
				tRange: { start: 300, stop: 1000, step: 10 },
				pressure: 101325,
			},
		},
		{
			components: ['AL', 'C'],
			dbFile: 'Al-C.tdb',
			defaults: {
				xRange: { start: 0, stop: 1, step: 0.02 },
				tRange: { start: 300, stop: 1000, step: 10 },
				pressure: 101325,
			},
		},
		{
			components: ['AL', 'CU'],
			dbFile: 'Al-Cu.tdb',
			defaults: {
				xRange: { start: 1e-5, stop: 1, step: 0.02 },
				tRange: { start: 300, stop: 2000, step: 10 },
				pressure: 101325,
			},
		},
		{
			components: ['MG', 'ND'],
			dbFile: 'Mg-Nd.tdb',
			defaults: {
				xRange: { start: 0, stop: 1, step: 0.01 },
				tRange: { start: 300, stop: 2000, step: 10 },
				pressure: 101325,
			},
		},
		{
			components: ['MG', 'SI'],
			dbFile: 'Mg-Si.tdb',
			defaults: {
				xRange: { start: 0, stop: 1, step: 0.01 },
				tRange: { start: 300, stop: 2000, step: 10 },
				pressure: 101325,
			},
		},
		{
			components: ['MG', 'ZN'],
			dbFile: 'Mg-Zn.tdb',
			defaults: {
				xRange: { start: 0, stop: 1, step: 0.01 },
				tRange: { start: 300, stop: 2000, step: 10 },
				pressure: 101325,
			},
		},
	],
	getMatchedCombination(components) {
		if (!components || !Array.isArray(components)) return null
		const sorted = [...components].sort()
		return this.validCombinations.find(
			(comb) =>
				JSON.stringify([...comb.components].sort()) === JSON.stringify(sorted)
		)
	},
}

// 三元计算数据库配置
export const ternaryDatabase = {
	configurations: [
		{ elements: ['AL', 'C', 'MG'], dbFile: 'Al-C-Mg.tdb' },
		{ elements: ['AL', 'CO', 'GD'], dbFile: 'Al-Co-Gd.tdb' },
		{ elements: ['AL', 'CO', 'W'], dbFile: 'Al-Co-W.tdb' },
		{ elements: ['AL', 'CU', 'SI'], dbFile: 'Al-Cu-Si.tdb' },
		{ elements: ['AL', 'CU', 'Y'], dbFile: 'Al-Cu-Y.tdb' },
		{ elements: ['AL', 'CU', 'ZN'], dbFile: 'Al-Cu-Zn.tdb' },
		{ elements: ['BI', 'MG', 'SN'], dbFile: 'Bi-Mg-Sn.tdb' },
		{ elements: ['MG', 'SN', 'SR'], dbFile: 'Mg-Sn-Sr.tdb' },
	],
	elementRelations: {
		AL: ['C', 'CO', 'CU'],
		BI: ['MG'],
		MG: ['SN'],
	},
	thirdElementRelations: {
		'AL-C': ['MG'],
		'AL-CO': ['GD', 'W'],
		'AL-CU': ['SI', 'Y', 'ZN'],
		'BI-MG': ['SN'],
		'MG-SN': ['SR'],
	},
	getMatchedConfiguration(elements) {
		if (!elements || elements.length !== 3) return null
		const sorted = [...elements].sort()
		return this.configurations.find(
			(config) =>
				JSON.stringify([...config.elements].sort()) === JSON.stringify(sorted)
		)
	},
}

// 动态获取所有元素（不区分二元三元）
export function getAllElements() {
	const set = new Set()
	// 二元
	binaryDatabase.validCombinations.forEach((item) => {
		item.components.forEach((e) => set.add(e))
	})
	// 三元
	ternaryDatabase.configurations.forEach((item) => {
		item.elements.forEach((e) => set.add(e))
	})
	return Array.from(set)
}

// 动态获取第二个元素
export function getSecondElements(first) {
	if (!first) return []
	const set = new Set()
	// 二元
	binaryDatabase.validCombinations.forEach((item) => {
		if (item.components.includes(first)) {
			item.components.forEach((e) => {
				if (e !== first) set.add(e)
			})
		}
	})
	// 三元
	ternaryDatabase.configurations.forEach((item) => {
		if (item.elements.includes(first)) {
			item.elements.forEach((e) => {
				if (e !== first) set.add(e)
			})
		}
	})
	return Array.from(set)
}

// 动态获取第三个元素（只针对三元）
export function getThirdElements(first, second) {
	if (!first || !second) return []
	const set = new Set()
	ternaryDatabase.configurations.forEach((item) => {
		if (item.elements.includes(first) && item.elements.includes(second)) {
			item.elements.forEach((e) => {
				if (e !== first && e !== second) set.add(e)
			})
		}
	})
	return Array.from(set)
}

// 顺序无关算法：获取所有涉及到的元素
export function getAllElementsUniversal() {
	const set = new Set()
	binaryDatabase.validCombinations.forEach((item) => {
		item.components.forEach((e) => set.add(e))
	})
	ternaryDatabase.configurations.forEach((item) => {
		item.elements.forEach((e) => set.add(e))
	})
	return Array.from(set)
}

// 顺序无关算法：二元，选第一个后推断第二个
export function getSecondElementsBinaryUniversal(first) {
	if (!first) return []
	const set = new Set()
	binaryDatabase.validCombinations.forEach((item) => {
		if (item.components.includes(first)) {
			item.components.forEach((e) => {
				if (e !== first) set.add(e)
			})
		}
	})
	return Array.from(set)
}

// 顺序无关算法：三元，选第一个后推断第二个
export function getSecondElementsTernaryUniversal(first) {
	if (!first) return []
	const set = new Set()
	ternaryDatabase.configurations.forEach((item) => {
		if (item.elements.includes(first)) {
			item.elements.forEach((e) => {
				if (e !== first) set.add(e)
			})
		}
	})
	return Array.from(set)
}

// 顺序无关算法：三元，选前两个后推断第三个
export function getThirdElementsTernaryUniversal(first, second) {
	if (!first || !second) return []
	const set = new Set()
	ternaryDatabase.configurations.forEach((item) => {
		if (item.elements.includes(first) && item.elements.includes(second)) {
			item.elements.forEach((e) => {
				if (e !== first && e !== second) set.add(e)
			})
		}
	})
	return Array.from(set)
}

// 元素选项配置
export const elementOptions = {
	binary: {
		first: [
			{ label: 'Al', value: 'AL' },
			{ label: 'Mg', value: 'MG' },
		],
		second: {
			AL: [
				{ label: 'Ag', value: 'AG' },
				{ label: 'C', value: 'C' },
				{ label: 'Cu', value: 'CU' },
			],
			MG: [
				{ label: 'Nd', value: 'ND' },
				{ label: 'Si', value: 'SI' },
				{ label: 'Zn', value: 'ZN' },
			],
		},
	},
	ternary: {
		first: [
			{ label: 'Al', value: 'AL' },
			{ label: 'Bi', value: 'BI' },
			{ label: 'Mg', value: 'MG' },
		],
		second: {
			AL: [
				{ label: 'C', value: 'C' },
				{ label: 'Co', value: 'CO' },
				{ label: 'Cu', value: 'CU' },
			],
			BI: [{ label: 'Mg', value: 'MG' }],
			MG: [{ label: 'Sn', value: 'SN' }],
		},
		third: {
			'AL-C': [{ label: 'Mg', value: 'MG' }],
			'AL-CO': [
				{ label: 'Gd', value: 'GD' },
				{ label: 'W', value: 'W' },
			],
			'AL-CU': [
				{ label: 'Si', value: 'SI' },
				{ label: 'Y', value: 'Y' },
				{ label: 'Zn', value: 'ZN' },
			],
			'BI-MG': [{ label: 'Sn', value: 'SN' }],
			'MG-SN': [{ label: 'Sr', value: 'SR' }],
		},
	},
}

// 表单配置
export const formConfig = {
	units: {
		temperature: ['开尔文'],
		pressure: ['帕', 'atm', 'bar'],
		composition: ['摩尔分数', '重量分数'],
	},
}

// 获取所有二元涉及到的元素
export function getAllElementsBinary() {
	const set = new Set()
	binaryDatabase.validCombinations.forEach((item) => {
		item.components.forEach((e) => set.add(e))
	})
	return Array.from(set)
}

// 获取所有三元涉及到的元素
export function getAllElementsTernary() {
	const set = new Set()
	ternaryDatabase.configurations.forEach((item) => {
		item.elements.forEach((e) => set.add(e))
	})
	return Array.from(set)
}
